define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {};
        },
        getModalList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{promotionPlanId:"spreadId",mediaId:"media"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.space.modalList(param.campaignId,newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        return $.ExtCommon.getMappingJson(info,{id:"id",name:"name",adFormat:"adFormatDesc",describe:"description",media:"mediaName", platform:"osTypeDesc"},true);
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataModalList: res});
                self.trigger('bind:ModalList');
            });
        },
        getMaterialInfo: function (id) {
            var self = this;
            servers.space.materialInfo(id, function (res) {
                var newJson = $.ExtCommon.getMappingJson(res.success ? res.data : {}, {
                    template: "templateId", adFormat:"adFormat",list:"elementList",sampleUrl:"sampleUrl"
                }, true);
                newJson.adFormat=newJson.adFormat.toString();
                newJson.isNative=newJson.adFormat==="5";
                if($.ExtCommon.isCheckEmpty(newJson.list)){newJson.list=[]}
                if(newJson.isNative){
                    newJson.list=$.map(newJson.list,function (info) {
                        var elementInfo= $.ExtCommon.getMappingJson(info, {
                            tSpecId: "id",parentId:"parentId",elementType:"elementType",elementName:"elementName",required:"required",fileFormat:"format",
                            width:"widthMin",height:"heightMax",size:"size",minOccurs:"minOccurs",maxOccurs:"maxOccurs",matterTypeFilter:"productType"
                        },true);
                        elementInfo.tSpecId=elementInfo.tSpecId.toString();
                        elementInfo.parentId=$.ExtCommon.isCheckEmpty(elementInfo.parentId)?"0":elementInfo.parentId.toString();
                        elementInfo.elementType=elementInfo.elementType.toString();
                        elementInfo.isImage=elementInfo.elementType==="1";
                        elementInfo.isText=elementInfo.elementType==="2";
                        elementInfo.isStruct=elementInfo.elementType==="3";
                        elementInfo.width=$.ExtCommon.isCheckEmpty(elementInfo.width)?0:parseInt(elementInfo.width);
                        elementInfo.height=$.ExtCommon.isCheckEmpty(elementInfo.height)?0:parseInt(elementInfo.height);
                        elementInfo.lableText="";
                        elementInfo.minLength=0;
                        elementInfo.maxLength=0;
                        if(elementInfo.isImage){elementInfo.lableText="图片"}
                        else if(elementInfo.isText){
                            elementInfo.minLength=elementInfo.width;
                            elementInfo.maxLength=elementInfo.height;
                            elementInfo.lableText="文案";
                            elementInfo.placeholder="请输入";
                            var placeholder="";
                            if(elementInfo.minLength>0){
                                placeholder+="大于"+elementInfo.minLength;
                                if(elementInfo.maxLength>0){ placeholder+="且"}
                            }
                            if(elementInfo.maxLength>0){placeholder+="小于"+elementInfo.maxLength}
                            elementInfo.placeholder=placeholder.length===0?"":("请输入"+placeholder+"的字节");
                        }
                        elementInfo.isRequired=elementInfo.required.toString()==="1";
                        elementInfo.fileFormat= $.ExtCommon.isCheckEmpty(elementInfo.fileFormat)?"jpg,png":elementInfo.fileFormat.replace(/[|]/g, ',');
                        elementInfo.minOccurs=$.ExtCommon.isCheckEmpty(elementInfo.minOccurs)?0:parseInt(elementInfo.minOccurs);
                        elementInfo.maxOccurs=$.ExtCommon.isCheckEmpty(elementInfo.maxOccurs)?0:parseInt(elementInfo.maxOccurs);
                        elementInfo.arrFilter= $.ExtCommon.isCheckEmpty(elementInfo.matterTypeFilter)?[]:elementInfo.matterTypeFilter.split(",");
                        elementInfo.tipMsg="";
                        if(elementInfo.isStruct){
                            elementInfo.tipMsg=elementInfo.minOccurs>0 && elementInfo.maxOccurs>1 &&  elementInfo.maxOccurs>=elementInfo.minOccurs ? ("请至少完善"+elementInfo.minOccurs+"组信息"):"";
                        }
                        return elementInfo;
                    });
                }else{
                    newJson.list=$.map(newJson.list,function (info) {
                        var elementInfo= $.ExtCommon.getMappingJson(info, {tSpecId: "id", width:"widthMin",height:"heightMax",size:"size",fileFormat:"format"},true);
                        elementInfo.isImage=true;
                        elementInfo.isText=false;
                        elementInfo.fileFormat= $.ExtCommon.isCheckEmpty(elementInfo.fileFormat)?"jpg,png":elementInfo.fileFormat.replace(/[|]/g, ',');
                        return elementInfo;
                    });
                }
                res.dataOriginal = res.data;
                res.data = newJson;
                self.set({dataMaterialInfo: res});
                self.trigger('bind:MaterialInfo');
            });
        }
    });
});